// pages/auth/registration/registration.js
const util=require('../../../utils/util')
const api =require('../../../config/api')
var app =getApp()
Page({

  data: {
    username:'123456',
    password:'123123',
    confirmPassword:'123123',
    phoneNumber:'15614375583',
    code:"123"
  },
  // 点击注册按钮 进行数据注册
  registration:function(){
    let that =this
    if(this.data.username.length<6 || this.data.password.length<6){
      wx.showModal({
        title: '错误信息',
        content: '用户名和密码不能少于6位',
        showCancel:false
      })
      return false
    }
    if(this.data.password !=this.data.confirmPassword){
      wx.showModal({
        title: '错误信息',
        content: '确认密码不一致',
        showCancel:false
      })
      return false
    }
    if (this.data.phoneNumber.length==0 ||this.data.code.length==0 ) {
      wx.showToast({
      title: '手机号和验证码不能为空',
      duration: 2000,
      icon:'none'
      });
      return false;
    }    
    if (!(/^1[34578]\d{9}$/.test(this.data.phoneNumber))) {
      wx.showToast({
      title: '手机号码有误',
      duration: 2000,
      icon:'none'
      });
      return false;
    }
    wx.login({
      success: (res) => {
        console.log(res.code);
        if(!res.code){
          wx.showModal({
            title: '',
            content: '注册失败',
            showCancel:false
          })
        }
        that.requestRegister(res.code)
      },
    }) 
  },
// 封装请求注册接口
requestRegister:function(wxcode){
  let that =this
  console.log(this.data.username);
  util.request(api.AuthRegister,
    {
      username:that.data.username,
      password:that.data.password,
      mobile:that.data.phoneNumber,
      code:that.data.code,
      wxCode:wxcode
    },'post').then(res=>{
      if(res.errno == 0){
        app.globalData.hasLogin=true
        wx.setStorageSync('userInfo', res.data.userInfo)
        wx.setStorage({
          key:"token", 
          data:res.data.token,
          success:function(){
            wx.switchTab({
              url: '/pages/ucenter/index/index',
            })
          }
        })
      }
      if(res.errno==704){
        wx.showToast({
          title: '用户名已注册',
          duration: 2000,
          icon:'none'
          });
      }
    console.log(res);
  }).catch(err=>{
    console.log('err',err);
  })
},

  // 一共十个， 五个input失焦事件 5个点击图片，清空input事件 
  username(e){
    this.setData({
      username:e.detail.value
    })
  },
  // 点击图片，清空input事件 5个
  usernameImage(){
    this.setData({
      username:''
    })
  },
  password(e){
    this.setData({
      password:e.detail.value
    })
  },
  passwordImage(){
    this.setData({
      password:''
    })
  },
  confirmPassword(e){
    this.setData({
      confirmPassword:e.detail.value
    })
  },
  confirmPasswordImage(){
    this.setData({
      confirmPassword:''
    })
  },
  phoneNumber(e){
    this.setData({
      phoneNumber:e.detail.value
    })
  },
  phoneNumberImage(){
    this.setData({
      phoneNumber:''
    })
  },
  code(e){
    this.setData({
      code:e.detail.value
    })
  },
  codeImage(){
    this.setData({
      code:''
    })
  },

  


  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})